<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>05_员工列表练习</title>
</head>
<body>
    <input type="text" placeholder="请输入员工姓名" id="i1">
    <input type="text" placeholder="请输入员工工资" id="i2">
    <input type="text" placeholder="请输入员工岗位" id="i3">
    <button onclick="addEmp()">点我添加</button>
    <hr>
    <table border="1">
        <caption>员工列表</caption>
        <tr>
            <th>员工姓名</th>
            <th>员工工资</th>
            <th>员工岗位</th>
        </tr>
    </table>
    <script>
        //定义一个数组用来保存多个员工对象数据
        let arr=[];
        //定义新增员工的方法
        function addEmp(){
            let uName=document.getElementById('i1').value;
            let uSalary=document.getElementById('i2').value;
            let uJob=document.getElementById('i3').value;
            /*对数据进行数据校验*/
            if(uName.trim()==''||uSalary.trim()==''||uJob.trim()==''){
                alert('请输入完整的员工数据！')
                return;
            }
            let trE=document.createElement('tr');
            let nameTd=document.createElement('td');
            let salaryTd=document.createElement('td');
            let jobTd=document.createElement('td');

            nameTd.innerHTML=uName;
            salaryTd.innerHTML=uSalary;
            jobTd.innerHTML=uJob;
            trE.append(nameTd,salaryTd,jobTd);
            //获取用来显示数据的元素表格
            let tableE=document.querySelector('table');
            tableE.append(trE);
            //将收集好的员工数据存入数组中，发给后端服务器，存入数据库中
            arr.push({
                name:uName,
                salary:uSalary,
                job:uJob
            });
            //清空输入框中的数据
            document.getElementById('i1').value='';
            document.getElementById('i2').value='';
            document.getElementById('i3').value='';
        }
    </script>
</body>
</html>